package com.example.android.course.sample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android.course.sample.fragments.ExchangeFragment;

public class MainActivity extends ActionBarActivity implements OnItemClickListener {

    private DrawerLayout drawerLayoutNavDrawer;
    private ArrayAdapter<String> drawerAdapter;
    private ListView listViewDrawerOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listViewDrawerOptions = (ListView) findViewById(R.id.listViewDrawerOptions);

        drawerAdapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, R.id.textViewListItemText,
                new String[] { "Option 1", "Option 2", "Option 3" });

        listViewDrawerOptions.setAdapter(drawerAdapter);

        drawerLayoutNavDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        listViewDrawerOptions.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

//    /**
//     * A placeholder fragment containing a simple view.
//     */
//    public static class PlaceholderFragment extends Fragment {
//
//        public PlaceholderFragment() {
//        }
//
//        @Override
//        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//            return rootView;
//        }
//    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View pressedView, int position, long id) {

        CharSequence text = ((TextView) pressedView.findViewById(R.id.textViewListItemText)).getText();
        String itemText = text == null ? "" : text.toString();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction();

        Fragment fragment = new ExchangeFragment();
        Bundle fragArgs = new Bundle();
        fragArgs.putString(ExchangeFragment.EXTRA_FRAGMENT_TEXTVIEW_TEXT, itemText);
        fragment.setArguments(fragArgs);

        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        drawerLayoutNavDrawer.closeDrawers();
    }

}
